<template>
	<view class="address">
		<dwu-navbar title="地址管理"></dwu-navbar>
		<view class="address-list" v-if="addressList.length > 0">
			<view class="address-item" v-for="(item,index) in addressList" :key="index">
				<view class="choose" v-if="delAll" @click="chooseItem(item)">
					<image v-if="item.checked" src="@/static/integral/checked.png" mode=""></image>
					<image v-else src="@/static/integral/check.png" mode=""></image>
				</view>
				<view class="info">
					<view class="title">
						<view class="name">
							{{item.name || '-'}}
						</view>
						<view class="phone">
							{{item.phone || '-'}}
						</view>
					</view>
					<view class="site">
						{{item.address || '-'}}
					</view>
					<view class="options">
						<view class="default" @click="setDefault(item)">
							<image v-if="item.mrAddress_text" src="@/static/integral/checked.png" mode=""></image>
							<image v-else src="@/static/integral/check.png" mode=""></image>
							<text>默认地址</text>
						</view>
						<view class="right">
							<view class="right-item" @click="onJump('/subpackage/address/add?id='+item.id)">
								<image src="@/static/address/edit_icon.png" mode=""></image>
								<text>编辑</text>
							</view>
							<view class="right-item" @click="delAddress(item)">
								<image src="@/static/address/del_icon.png" mode=""></image>
								<text>删除</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view v-else class="no-data">
			<image src="@/static/address/no_data.png" mode=""></image>
			<text>空空如也，新增地址开启体验吧~</text>
		</view>
		<view class="btns" v-if="delAll">
			<view class="btns-item" @click="delAll = !delAll">
				完成
			</view>
			<view class="btns-item" :class="addressList.length == 0 ? 'none' : ''" @click="delAddress">
				删除
			</view>
		</view>
		<view class="btns" v-else>
			<view class="btns-item" v-if="addressList.length > 0" @click="delAll = !delAll">
				管理
			</view>
			<view class="btns-item" :class="addressList.length == 0 ? 'none' : ''"
				@click="onJump('/subpackage/address/add')">
				新增地址
			</view>
		</view>
		<dwu-modal ref="dwuModal"></dwu-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [],
				delAll: false,
				page: 1,
				pageSize: 10,
				flag: false,
			};
		},
		onLoad() {
			this.addressIndex()
		},
		onReachBottom() {
			if (this.flag) return
			this.page++
			this.flag = true
			this.addressIndex(true)
		},
		methods: {
			reset() {
				this.page = 1
				this.flag = false
				this.delAll = false
				this.addressIndex()
			},
			addressIndex(next) {
				this.request.http('/user/addressIndex', 'post', {
					page: this.page,
					limit: this.pageSize,
				}).then(res => {
					let data = res.data
					if (data.code == 1) {
						if (next) {
							if (data.data.length > 0) {
								this.addressList = this.addressList.concat(data.data)
								this.flag = false
							}
						} else {
							this.addressList = data.data
							this.flag = false
						}
					}
				})
			},
			delAddress(item) {
				this.$refs.dwuModal.open({
					content: item.id ? '是否删除当前地址？' : '是否删除选中地址？',
					type: 'del',
					success: (res) => {
						let delIdArr = []
						let delIds = ''
						if (item.id) {
							delIds = item.id
						} else {
							let addressList = this.addressList
							for (var i = 0; i < addressList.length; i++) {
								if (addressList[i].checked) {
									delIdArr.push(addressList[i].id)
								}
							}
							delIds = delIdArr.join(',')
						}
						console.log(delIds);
						// return
						this.request.http('/user/addressDel', 'post', {
							id: delIds,
						}).then(res => {
							let data = res.data
							if (data.code == 1) {
								this.reset()
							}
						})
					}
				})
			},
			chooseItem(item) {
				item.checked = !item.checked
				this.$forceUpdate()
			},
			setDefault(item) {
				if (item.mrAddress_text == 1) return
				this.$refs.dwuModal.open({
					content: '是否设置当前地址为默认地址？',
					success: (res) => {
						this.request.http('/user/addressAcquiesce', 'post', {
							id: item.id,
							is_type: 1
						}).then(res => {
							let data = res.data
							if (data.code == 1) {
								this.reset()
							}
						})
					}
				})
			},
			onJump(url) {
				uni.navigateTo({
					url
				})
			}
		}
	}
</script>

<style lang="less">
	page {
		background: #F7F9F9;
	}

	.address {
		padding: 20rpx;
		padding-bottom: 180rpx;

		.address-list {
			.address-item {
				display: flex;
				align-items: center;
				margin-bottom: 20rpx;

				.choose {
					flex-shrink: 0;
					margin-right: 30rpx;

					image {
						width: 34rpx;
						height: 34rpx;
						flex-shrink: 0;
					}
				}

				.info {
					flex: 1;
					padding: 24rpx 32rpx;
					background: #FFFFFF;
					box-shadow: 0rpx 4rpx 13rpx 8rpx rgba(245, 245, 245, 0.16);
					border-radius: 35rpx;

					.title {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.name {
							font-family: PingFangSC, PingFang SC;
							font-weight: 600;
							font-size: 31rpx;
							color: #474747;
						}

						.phone {
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 27rpx;
							color: #999999;
						}
					}

					.site {
						padding: 20rpx 0 42rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 27rpx;
						color: #999999;
						line-height: 38rpx;
						border-bottom: 1px solid rgba(151, 151, 151, 0.18);
					}

					.options {
						padding: 20rpx 0 0;
						display: flex;
						align-items: center;
						justify-content: space-between;

						.default {
							display: flex;
							align-items: center;

							image {
								width: 25rpx;
								height: 25rpx;
								margin-right: 12rpx;
							}

							text {
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 27rpx;
								color: #999999;
							}
						}

						.right {
							display: flex;
							align-items: center;

							.right-item {
								display: flex;
								align-items: center;

								image {
									width: 22rpx;
									height: 22rpx;
									margin-right: 10rpx;
								}

								text {
									font-family: PingFangSC, PingFang SC;
									font-weight: 400;
									font-size: 23rpx;
									color: #999999;
								}

								&:last-child {
									margin-left: 38rpx;
								}
							}
						}
					}
				}
			}
		}

		.no-data {
			padding-top: 240rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;

			image {
				width: 282rpx;
				height: 292rpx;
				margin-bottom: 26rpx;
			}

			text {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 23rpx;
				color: #999999;
				line-height: 33rpx;
			}
		}

		.btns {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 50rpx 0;
			// background-color: #F7F9F9;

			.btns-item {
				width: 292rpx;
				height: 80rpx;
				border-radius: 40rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 27rpx;
				text-align: center;
				line-height: 80rpx;

				&:first-child {
					margin-right: 40rpx;
					background-color: #fff;
					color: #666666;
					box-shadow: 1px 1px 1px rgba(245, 245, 245, 0.16);
				}

				&:last-child {
					background-color: #3D3D3D;
					color: #B0DD67;

					&.none {
						width: 650rpx;
						height: 104rpx;
						line-height: 104rpx;
						margin-right: 0;
					}
				}
			}
		}
	}
</style>